// Copyright (c) 2011, Marc-Andre Ferland - madrang@gmail.com
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the The Warrent Team nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//     * This software is free for non-commercial use. You may not use this
//       software, in whole or in part, in support of any commercial product
//       without the express consent of the author.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE WARRENT TEAM BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System;
using NUnit.Framework;
using OsHelper.UsbNet.Descriptors;

namespace UsbNUnit
{
	[TestFixture()]
	public class ConfigurationDescriptorNUnit
	{
		[Test()]
		public void FromByteNoInterface ()
		{
			byte[] ConfDescByte = new byte[]{
				//Length
				9,
				//Configuration Descriptor Type
				2,
				//TotalLength = 0
				0x09, 0x00,
				//NumberInterfaces
				0,
				//ConfigurationValue
				1,
				//StringIndex
				24,
				//Attributes
				// Bits [0, 4] Reserved (reset to zero)
				// Bit 5: Remote Wakeup = true
				// Bit 6: Self-powered = false
				// Bit 7: Reserved (set to one)
				0xA0,
				//MaxPower = 200mA
				100
			};
			
			ConfigurationDescriptor ConfDesc = ConfigurationDescriptor.FromBytes (ConfDescByte);
			
			Assert.AreEqual(9, ConfDesc.Length, "Length");
			Assert.AreEqual(2, (byte)ConfDesc.Type, "Type");
			Assert.AreEqual(9, ConfDesc.TotalLength, "TotalLength");
			Assert.AreEqual(0, ConfDesc.NumberInterfaces, "NumberInterfaces");
			Assert.AreEqual(1, ConfDesc.ConfigurationValue, "ConfigurationValue");
			Assert.AreEqual(24, ConfDesc.StringIndex, "StringIndex");
			Assert.AreEqual(true, ConfDesc.RemoteWakeup, "RemoteWakeup");
			Assert.AreEqual(false, ConfDesc.SelfPowered, "SelfPowered");
			Assert.AreEqual(0.200d, ConfDesc.MaxPower, "MaxPower");
		}
		
		[Test()]
		public void FromByteClean ()
		{
			byte[] ConfDescByte = new byte[]{
				//Length
				9,
				//Configuration Descriptor Type
				2,
				//TotalLength = 27
				0x1B, 0x00,
				//NumberInterfaces
				2,
				//ConfigurationValue
				1,
				//StringIndex
				24,
				//Attributes
				// Bits [0, 4] Reserved (reset to zero)
				// Bit 5: Remote Wakeup = true
				// Bit 6: Self-powered = false
				// Bit 7: Reserved (set to one)
				0xA0,
				//MaxPower = 200mA
				100,
				
				//Interface 1
				//Length
				9,
				//Interface Descriptor Type
				4,
				
				//Number
				1,

				//AlternateSetting
				0,

				//NumberEndpoints
				0,
				
				//ClassCodeType Class = DiagnosticDevice;
				0xDC,

				//SubClass
				0xAF,
				
				//Protocol
				0xCD,
				
				//StringIndex
				12,
				
				//Interface 2
				//Length
				9,
				//Interface Descriptor Type
				4,
				
				//Number
				1,

				//AlternateSetting
				1,

				//NumberEndpoints
				0,
				
				//ClassCodeType Class = Miscellaneous;
				0xEF,

				//SubClass
				0xCF,
				
				//Protocol
				0xCC,
				
				//StringIndex
				20,
			};
			
			ConfigurationDescriptor ConfDesc = ConfigurationDescriptor.FromBytes (ConfDescByte);
			
			Assert.AreEqual(9, ConfDesc.Length, "Length");
			Assert.AreEqual(2, (byte)ConfDesc.Type, "Type");
			Assert.AreEqual(27, ConfDesc.TotalLength, "TotalLength");
			Assert.AreEqual(2, ConfDesc.NumberInterfaces, "NumberInterfaces");
			Assert.AreEqual(1, ConfDesc.ConfigurationValue, "ConfigurationValue");
			Assert.AreEqual(24, ConfDesc.StringIndex, "StringIndex");
			Assert.AreEqual(true, ConfDesc.RemoteWakeup, "RemoteWakeup");
			Assert.AreEqual(false, ConfDesc.SelfPowered, "SelfPowered");
			Assert.AreEqual(0.200d, ConfDesc.MaxPower, "MaxPower");
			Assert.AreEqual(2, ConfDesc.InterfaceList.Count, "InterfaceList");
		}
	}
}
